home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxsample.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  6.0 KB  |  239 lines

  1. /* Copyright (C) 1997, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxsample.c,v 1.2 2000/09/19 19:00:40 lpd Exp $ */
  20. /* Sample unpacking procedures */
  21. #include "gx.h"
  22. #include "gxsample.h"
  23.  
  24. /* ---------------- Lookup tables ---------------- */
  25.  
  26. /*
  27.  * Define standard tables for spreading 1-bit input data.
  28.  * Note that these depend on the end-orientation of the CPU.
  29.  * We can't simply define them as byte arrays, because
  30.  * they might not wind up properly long- or short-aligned.
  31.  */
  32. #define map4tox(z,a,b,c,d)\
  33.     z, z^a, z^b, z^(a+b),\
  34.     z^c, z^(a+c), z^(b+c), z^(a+b+c),\
  35.     z^d, z^(a+d), z^(b+d), z^(a+b+d),\
  36.     z^(c+d), z^(a+c+d), z^(b+c+d), z^(a+b+c+d)
  37. /* Work around warnings from really picky compilers. */
  38. #ifdef __STDC__
  39. #  define n0L 0xffffffffU
  40. #  define ffL8 0x0000ff00U
  41. #  define ffL16 0x00ff0000U
  42. #  define ffL24 0xff000000U
  43. #else
  44. #if arch_sizeof_long == 4
  45. /*
  46.  * The compiler evaluates long expressions mod 2^32.  Even very picky
  47.  * compilers allow assigning signed longs to unsigned longs, so we use
  48.  * signed constants.
  49.  */
  50. #  define n0L (-1)
  51. #  define ffL8 0x0000ff00
  52. #  define ffL16 0x00ff0000
  53. #  define ffL24 (-0x01000000)
  54. #else
  55. /*
  56.  * The compiler evaluates long expressions mod 2^64.
  57.  */
  58. #  define n0L 0xffffffffL
  59. #  define ffL8 0x0000ff00L
  60. #  define ffL16 0x00ff0000L
  61. #  define ffL24 0xff000000L
  62. #endif
  63. #endif
  64. #if arch_is_big_endian
  65. const bits32 lookup4x1to32_identity[16] = {
  66.     map4tox(0, 0xff, ffL8, ffL16, ffL24)
  67. };
  68. const bits32 lookup4x1to32_inverted[16] = {
  69.     map4tox(n0L, 0xff, ffL8, ffL16, ffL24)
  70. };
  71. #else /* !arch_is_big_endian */
  72. const bits32 lookup4x1to32_identity[16] = {
  73.     map4tox(0, ffL24, ffL16, ffL8, 0xff)
  74. };
  75. const bits32 lookup4x1to32_inverted[16] = {
  76.     map4tox(n0L, ffL24, ffL16, ffL8, 0xff)
  77. };
  78. #endif
  79. #undef n0L
  80. #undef ffL8
  81. #undef ffL16
  82. #undef ffL24
  83.  
  84. /* ---------------- Unpacking procedures ---------------- */
  85.  
  86. const byte *
  87. sample_unpack_copy(byte * bptr, int *pdata_x, const byte * data, int data_x,
  88.         uint dsize, const sample_lookup_t * ignore_ptab, int spread)
  89. {                /* We're going to use the data right away, so no copying is needed. */
  90.     *pdata_x = data_x;
  91.     return data;
  92. }
  93.  
  94. const byte *
  95. sample_unpack_1(byte * bptr, int *pdata_x, const byte * data, int data_x,
  96.         uint dsize, const sample_lookup_t * ptab, int spread)
  97. {
  98.     const byte *psrc = data + (data_x >> 3);
  99.     int left = dsize - (data_x >> 3);
  100.  
  101.     if (spread == 1) {
  102.     bits32 *bufp = (bits32 *) bptr;
  103.     const bits32 *map = &ptab->lookup4x1to32[0];
  104.     uint b;
  105.  
  106.     if (left & 1) {
  107.         b = psrc[0];
  108.         bufp[0] = map[b >> 4];
  109.         bufp[1] = map[b & 0xf];
  110.         psrc++, bufp += 2;
  111.     }
  112.     left >>= 1;
  113.     while (left--) {
  114.         b = psrc[0];
  115.         bufp[0] = map[b >> 4];
  116.         bufp[1] = map[b & 0xf];
  117.         b = psrc[1];
  118.         bufp[2] = map[b >> 4];
  119.         bufp[3] = map[b & 0xf];
  120.         psrc += 2, bufp += 4;
  121.     }
  122.     } else {
  123.     byte *bufp = bptr;
  124.     const byte *map = &ptab->lookup8[0];
  125.  
  126.     while (left--) {
  127.         uint b = *psrc++;
  128.  
  129.         *bufp = map[b >> 7];
  130.         bufp += spread;
  131.         *bufp = map[(b >> 6) & 1];
  132.         bufp += spread;
  133.         *bufp = map[(b >> 5) & 1];
  134.         bufp += spread;
  135.         *bufp = map[(b >> 4) & 1];
  136.         bufp += spread;
  137.         *bufp = map[(b >> 3) & 1];
  138.         bufp += spread;
  139.         *bufp = map[(b >> 2) & 1];
  140.         bufp += spread;
  141.         *bufp = map[(b >> 1) & 1];
  142.         bufp += spread;
  143.         *bufp = map[b & 1];
  144.         bufp += spread;
  145.     }
  146.     }
  147.     *pdata_x = data_x & 7;
  148.     return bptr;
  149. }
  150.  
  151. const byte *
  152. sample_unpack_2(byte * bptr, int *pdata_x, const byte * data, int data_x,
  153.         uint dsize, const sample_lookup_t * ptab, int spread)
  154. {
  155.     const byte *psrc = data + (data_x >> 2);
  156.     int left = dsize - (data_x >> 2);
  157.  
  158.     if (spread == 1) {
  159.     bits16 *bufp = (bits16 *) bptr;
  160.     const bits16 *map = &ptab->lookup2x2to16[0];
  161.  
  162.     while (left--) {
  163.         uint b = *psrc++;
  164.  
  165.         *bufp++ = map[b >> 4];
  166.         *bufp++ = map[b & 0xf];
  167.     }
  168.     } else {
  169.     byte *bufp = bptr;
  170.     const byte *map = &ptab->lookup8[0];
  171.  
  172.     while (left--) {
  173.         unsigned b = *psrc++;
  174.  
  175.         *bufp = map[b >> 6];
  176.         bufp += spread;
  177.         *bufp = map[(b >> 4) & 3];
  178.         bufp += spread;
  179.         *bufp = map[(b >> 2) & 3];
  180.         bufp += spread;
  181.         *bufp = map[b & 3];
  182.         bufp += spread;
  183.     }
  184.     }
  185.     *pdata_x = data_x & 3;
  186.     return bptr;
  187. }
  188.  
  189. const byte *
  190. sample_unpack_4(byte * bptr, int *pdata_x, const byte * data, int data_x,
  191.         uint dsize, const sample_lookup_t * ptab, int spread)
  192. {
  193.     byte *bufp = bptr;
  194.     const byte *psrc = data + (data_x >> 1);
  195.     int left = dsize - (data_x >> 1);
  196.     const byte *map = &ptab->lookup8[0];
  197.  
  198.     while (left--) {
  199.     uint b = *psrc++;
  200.  
  201.     *bufp = map[b >> 4];
  202.     bufp += spread;
  203.     *bufp = map[b & 0xf];
  204.     bufp += spread;
  205.     }
  206.     *pdata_x = data_x & 1;
  207.     return bptr;
  208. }
  209.  
  210. const byte *
  211. sample_unpack_8(byte * bptr, int *pdata_x, const byte * data, int data_x,
  212.         uint dsize, const sample_lookup_t * ptab, int spread)
  213. {
  214.     byte *bufp = bptr;
  215.     const byte *psrc = data + data_x;
  216.  
  217.     *pdata_x = 0;
  218.     if (spread == 1) {
  219.     if (ptab->lookup8[0] != 0 ||
  220.         ptab->lookup8[255] != 255
  221.         ) {
  222.         uint left = dsize - data_x;
  223.         const byte *map = &ptab->lookup8[0];
  224.  
  225.         while (left--)
  226.         *bufp++ = map[*psrc++];
  227.     } else {        /* No copying needed, and we'll use the data right away. */
  228.         return psrc;
  229.     }
  230.     } else {
  231.     int left = dsize - data_x;
  232.     const byte *map = &ptab->lookup8[0];
  233.  
  234.     for (; left--; psrc++, bufp += spread)
  235.         *bufp = map[*psrc];
  236.     }
  237.     return bptr;
  238. }
  239.